layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    /**
     * 用户晒物管理
     */
    var Present = {
        tableId: "presentTable"
    };

    /**
     * 初始化表格的列
     */
    Present.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id'},
            {field: 'goodsId', hide: true,sort: true, title: '商品id'},
            {field: 'goodsNo', sort: true, title: '商品编号'},
            {field: 'goodsName', sort: true, title: '商品名',event:'goodsDetail',
                templet: function(d){
                    return '<span style="color: #01AAED;">'+ d.goodsName +'</span>'
                }
            },
            {field: 'playerId', hide: true,sort: true, title: '用户id'},
            {field: 'userName', sort: true, title: '用户名称'},
            {field: 'text', sort: true, title: '内容'},
           /* {field: 'watch', sort: true, title: '浏览量'},*/
            {field: 'praise', sort: true, title: '点赞数',align:"center",event:'praisePage',
                templet: function(d){
                    return '<span style="color: #01AAED;">'+ d.praise +'</span>'
                }
            },
            {field: 'commentCount', sort: true, title: '评论数',align:"center",event:'commentPage',
                templet: function(d){
                    return '<span style="color: #01AAED;">'+ d.commentCount +'</span>'
                }
            },
            {field: 'markName', sort: true, title: '标识',align:'center'},
            {field: 'presentStatus', sort: true, title: '状态',templet:"#statusTpl" ,align:"center"},
            {field: 'adminName', sort: true, title: '处理人'},
            {field: 'content', sort: true, title: '备注'},
            {field: 'offlineTime', sort: true,hide: true, title: '下线时间'},
            {field: 'handleTime', sort: true, title: '处理时间'},
            {field: 'deptid', sort: true,hide: true, title: '所属部门'},
            {field: 'createTime', sort: true, title: '新增时间'},
            {field: 'updateTime', sort: true, hide: true,title: '修改时间'},
            {field: 'recordStatus', hide: true,sort: true, title: '记录状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth:180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Present.search = function () {
        var queryData = {};
        queryData['goodsNo'] = $("#goodsNo").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['mark'] = $("#mark").val();
        queryData['status'] = $("#status").val();
        queryData['pubBeginTime'] = $("#pubBeginTime").val();
        queryData['pubEndTime'] = $("#pubEndTime").val();
        queryData['userName'] = $("#userName").val();
        table.reload(Present.tableId, {where: queryData,
            page:{
                curr:1
            }
        });
    };


    /**
     * 弹出添加对话框
     */
    Present.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加用户晒物',
            content: Feng.ctxPath + '/present/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Present.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Present.exportExcel = function () {
        var checkRows = table.checkStatus(Present.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Present.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        admin.open({
            type: 2,
            area:["70%","70%"],
            title: '修改用户晒物',
            maxmin:true,
            content: Feng.ctxPath + '/present/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Present.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Present.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/present/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Present.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /**
     * 点击查看详情
     * @param data
     */

    Present.openDetailDlg = function (data) {
        admin.putTempData('formOk', false);
        if(data.goodsName=="商品不存在"){
            return false;
        }
        admin.open({
            type: 2,
            title: '商品信息',
            anim: 1,
            area:["70%","70%"],
            maxmin:'true',
            content: Feng.ctxPath + '/goods/goodsDetail?id=' + data.goodsId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Present.tableId);
            }
        });
    };


    /**
     * 点击查看评论
     * @param data
     */
    Present.openCommentDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '评论',
            maxmin:true,
            area:["80%","70%"],
            content: Feng.ctxPath + '/comment/?id=' + data.id+"&commentType=3",
            end: function () {
                admin.getTempData('formOk') && table.reload(Present.tableId);
            }
        });
    };


    /**
     * 点赞列表
     * @param data
     */
    Present.openPraiseDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '点赞',
            maxmin:true,
            area:["80%","70%"],
            content: Feng.ctxPath + '/works/praise?id=' + data.id+"&commentType=3",
            end: function () {
                admin.getTempData('formOk') && table.reload(Present.tableId);
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Present.tableId,
        url: Feng.ctxPath + '/present/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Present.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Present.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Present.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Present.exportExcel();
    });

    //渲染时间选择框
    laydate.render({
            elem: '#pubBeginTime',
        max: Feng.currentDate()
    });
    laydate.render({
            elem: '#pubEndTime',
        max: Feng.currentDate()
    });

    // 工具条点击事件
    table.on('tool(' + Present.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Present.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Present.onDeleteItem(data);
        }else if (layEvent === 'goodsDetail') {
            Present.openDetailDlg(data);
        }else if(layEvent === 'praisePage'){
            Present.openPraiseDlg(data);
        }else if(layEvent === 'commentPage'){
            Present.openCommentDlg (data);
        }
    });
});
